<?php

namespace weixin\Controller;
use User\Api\UserApi;
use Think\Log;

/**
 * 用户控制器
 * 包括用户中心，用户登录及注册
 */
class UserController extends Controller {
    
    
    public function  user(){
        $uid=18;
        if ($uid){
            $where['uid']=$uid;
            $model=M("Member");
            $res=$model->where($where)->find();
            $this->assign("user",$res);
            $this->display();
        }
        
        
    }    

    public function  add_address(){
        $uid=18;
        if($uid){
            $aid = I('aid');
            $model=M("UserAddress");
            $data = $model->create();
            $data['uid'] = $uid;
            if(empty($aid)){//新增
                $model->add($data);
            }else{
                $model->save($data);
            }
            $where['uid'] = $uid;
            $res = $model->where($where)->select();
            $this->success($res);
        }else {
            $this->error("用户未登录");
        }
    }
    
    
    public  function add_card() {
        $uid=18;
        if($uid){
            $modelCar=M('UserCar');
            $where['uid']=$uid;
            $date=$modelCar->create();
            if($date['c_lcn_no']==''){
                $this->error("请填写车牌号");
            }
            if ($date['c_vhl_frm']==''){
                $this->error("请填写车架号");
            }
            if ($date['c_eng_no']==''){
                $this->error("请填写发动机号");
            }
            if ($date['c_fst_reg_dte']==''){
                $this->error("请填写登记日期");
            }
            $date['brand_id']=$_POST['brand'];
            $date['series_id']=$_POST['series'];
            $date['model_id']=$_POST['model'];
            $date['insurer_id']=$_POST['insurer'];
            $modelCar->where($where)->save($date);
            $this->redirect('car');
        }else {
            $this->error("用户未登录");
        }
    }
    
    public function add_password(){
        $uid=18;
        if ( IS_POST ) {
            $model = M('Member');
            $where['uid'] = $uid;
            $cashpass = $model->where($where)->getFieldById($uid,'cash_passwd');
            if(empty($cashpass)){
                $repassword = I('recpass');
                $newpass = I('newcpass');
                empty($newpass) && $this->error('请输入支付密码');
                empty($repassword) && $this->error('请输入确认支付密码');
                if($newpass !== $repassword){
                    $this->error('您输入的新密码与确认密码不一致');
                }
                $hash_pass = think_md5($newpass, C('DATA_AUTH_KEY'));
                $data['cash_passwd'] = $hash_pass;
                $model->where($where)->save($data);
                $this->success('设置成功！');
        
            }else{
                $password   =   I('oldcpass');
                $repassword = I('recpass');
                $newpass = I('newcpass');
                empty($password) && $this->error('请输入原密码');
                empty($newpass) && $this->error('请输入新密码');
                empty($repassword) && $this->error('请输入确认密码');
        
                if($newpass !== $repassword){
                    $this->error('您输入的新密码与确认密码不一致');
                }
                $hash_pass = think_md5($password, C('DATA_AUTH_KEY'));
                if($hash_pass != $cashpass){
                    $this->error('原有支付密码不正确！');
                }else{
                    $hash_pass = think_md5($newpass, C('DATA_AUTH_KEY'));
                    $data['cash_passwd'] = $hash_pass;
                    $model->where($where)->save($data);
                    $this->success('修改成功！');
                }
            }
            $this->error($cashpass);
        }else{
            $this->error('无效的请求!');
        }
    }
    
    public function  agreement(){
        
    }
    
    public  function coupon() {
        
        
        
        
        ;
    }
}


?>
